//
//  MyselfViewController.swift
//  FastDevelop
//
//  Created by IOS1 on 2025/6/27.
//

import UIKit

class MyselfViewController: HalfViewViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    @objc private func clickLogView(){
        
        if let _ = UserDefaultsManager.getString(forKey: .token) {
            
            self.navigationController?.pushViewController(AccountViewController(), animated: true)
        }else{
            
            self.navigationController?.pushViewController(LoginViewViewController(), animated: true)
        }
    }
    
    private func setupUI(){
        
        bottomView.addSubviews([
            logView,
            versionView
        ])
        logView.addSubviews([
            accountImageV,
            accountLabel
        ])
        versionView.addSubviews([
            versionImageV,
            versionLabel,
            valueLabel
        ])
        
        logView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(30)
            make.trailing.equalToSuperview().offset(-30)
            make.height.equalTo(52)
            make.top.equalToSuperview().offset(36)
        }
        versionView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(30)
            make.trailing.equalToSuperview().offset(-30)
            make.height.equalTo(52)
            make.top.equalTo(logView.snp.bottom).offset(10)
        }
        accountImageV.snp.makeConstraints { make in
            make.width.height.equalTo(32)
            make.leading.equalToSuperview().offset(18)
            make.centerY.equalToSuperview()
        }
        accountLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(64)
            make.centerY.equalToSuperview()
        }
        versionImageV.snp.makeConstraints { make in
            make.width.height.equalTo(32)
            make.leading.equalToSuperview().offset(18)
            make.centerY.equalToSuperview()
        }
        versionLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(64)
            make.centerY.equalToSuperview()
        }
        valueLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-20)
            make.centerY.equalToSuperview()
        }
    }
    
    private lazy var logView: ShadowView = {
        let v = ShadowView()
        v.cornerRadius = 8
        v.shadowColor = UIColor(hexString: "#333333", alpha: 0.5)
        v.shadowOpacity = 0.5
        v.shadowOffset = CGSize(width: 0, height: 2)
        v.shadowRadius = 5
        v.backgroundColor = .white
        let logTap = UITapGestureRecognizer(target: self, action: #selector(clickLogView))
        v.isUserInteractionEnabled = true
        v.addGestureRecognizer(logTap)
        return v
    }()
    private lazy var versionView: ShadowView = {
        let v = ShadowView()
        v.cornerRadius = 8
        v.shadowColor = UIColor(hexString: "#333333", alpha: 0.5)
        v.shadowOpacity = 0.5
        v.shadowOffset = CGSize(width: 0, height: 2)
        v.shadowRadius = 5
        v.backgroundColor = .white
        return v
    }()
    private lazy var accountImageV: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.image = UIImage(named: "mine_account")
        return v
    }()
    private lazy var versionImageV: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.image = UIImage(named: "mine_version")
        return v
    }()
    private lazy var accountLabel: UILabel = {
        let v = UILabel()
        v.textColor = UIColor(hexString: "#404347")
        v.font = UIFont.systemFont(ofSize: 17)
        if let uid = UserDefaultsManager.getString(forKey: .token) {
            v.text = "Konto"
        }else{
            v.text = "Registrieren/Anmelden"
        }
        return v
    }()
    private lazy var versionLabel: UILabel = {
        let v = UILabel()
        v.textColor = UIColor(hexString: "#404347")
        v.font = UIFont.systemFont(ofSize: 17)
        v.text = "Version"
        return v
    }()
    private lazy var valueLabel: UILabel = {
        let v = UILabel()
        v.textColor = UIColor(hexString: "#A1A1A1")
        v.font = UIFont.systemFont(ofSize: 17)
        if let ver = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String {
            v.text = "v" + ver
        }
        v.textAlignment = .right
        return v
    }()
}
